/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package rekeningrijdenserver;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.HashMap;
import util.Message;
import util.NetwerkComponent;
import util.Sniffer;


/**
 *
 * @author Jonas & Sam
 */
public class RekeningRijdenServerDummy implements RekeningRijdenServer {
    private static int IDCounter=0;
    private String ID;
    private PublicKey publicServerKey;
    private PrivateKey privateServerKey;
    private Sniffer sniffy;

    public RekeningRijdenServerDummy() {
        ID = "RRS-id"+IDCounter;
        IDCounter++;
    }

    public void sendMessage(Message msg, NetwerkComponent sender) {
        if(sniffy != null)
            sniffy.readTransmission(msg,sender,this);
        System.out.println(ID+" received message:\n"+msg+"\n\tFrom "+sender);
        HashMap<String,Object> fields = new HashMap<String,Object>();
        fields.put("data", "ok from "+ID);
        sender.sendMessage(new Message(Message.MessageType.DUMMY,fields),this);
    }

    @Override
    public String toString() {
        return ID;
    }

    // keypair generation
    private void GenerateOwnKeys(){

        try{

        // Generate Assymetric keypair via RSA with 2048 bits
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
        kpg.initialize(2048);
        KeyPair kp = kpg.genKeyPair();

        // store keys
        publicServerKey = kp.getPublic();
        privateServerKey = kp.getPrivate();

        } catch (NoSuchAlgorithmException e) {
        }
    }

    public PublicKey Ping() {
        // returns Server PublicKey
        return this.publicServerKey;
    }

    public void addSniffer(Sniffer sniffer) {
        sniffy = sniffer;
        System.out.println("Sniffer added");
    }

    public void removeSniffer() {
        sniffy = null;
        System.out.println("Sniffer removed");
    }

    public int[] getCategoriesPhysical() {
        return null;
    }
}
